// /////////////////////////////////////////////////////////////////////////////
//
// Name:            Communicator.h
// Author:          Michael Bartsch
//
// Desc :           The interface for the CCommunicator class.
//
// License:         Copyright (C) 2008-2009 Michael Bartsch
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the GNU General Public
//                  License as published by the Free Software Foundation,
//                  either version 3 of the License, or (at your option) any
//                  later version.
//
//                  This program is distributed in the hope that it will be
//                  useful, but WITHOUT ANY WARRANTY; without even the implied
//                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//                  PURPOSE. See the GNU General Public License for more
//                  details.
//
//                  You should have received a copy of the GNU General Public
//                  License along with this program. If not, see
//                  <http://www.gnu.org/licenses/>.
//
// /////////////////////////////////////////////////////////////////////////////

#ifndef __C_COMMUNICATOR_H_INCLUDED__
#define __C_COMMUNICATOR_H_INCLUDED__

// Include required header file(s).
#include "../App/App.h"
#include "./AICore.h"

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/thread/xtime.hpp>


// Forward Declarations.
class CCommunicator;


// The CCommunicator class.
//! Class that is responsible for the serial connection between Aia|CP and the
//! Aia.
//!
//! @note A singleton metatable with the same functionalities as the
//! CCommunicator class can be accessed through the Lua Scripting
//! language. The functions of this metatable can be accessed with
//! Communicator:<function> (notice the colon!). If the end-user
//! does not wish to use the Communicator metatable in Lua he/she
//! can decide to use com<function>.
//!
//! @see CommunicatorScript.cpp CommunicatorScript.h
class CCommunicator
{
public:

    //! Constructor
    CCommunicator();
    //! Deconstructor
    ~CCommunicator();

    //! Returns an instance of the singleton of CCommunicator.
    static CCommunicator* Instance();

    //! Continuously checks the serial connection between Aia|CP and the Aia
    //! for new data. If new data has been sent to Aia|CP it will be processed
    //! here.
    //!
    //! @note serialReader() runs in a separate thread than the main thread.
    //! This thread is created the first time a connection is established
    //! between Aia|CP and the Aia.
    void serialReader();

    //! Writes the given command via the serial connection to the Aia.
    //! @see lua_com_serialWrite
	void serialWrite(const char *command);
    //! Attempt to reconnect with the Aia.
    //! @see lua_com_serialReconnect
	void serialConnect(int device);

	//! Gets the currently active device.
	//! @see lua_com_getSelectedDevice
	int getSelectedDevice();


private:

    // CCommunicator singleton.
	static CCommunicator *pInstance;

	boost::asio::io_service ioService;
	boost::asio::serial_port *serialPort;

	boost::thread *threadRead;

    int selectedDevice;
	bool isSerialConnection;
	bool isReadThread;

};

#endif
